package inline.core;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * Simple notes database access helper class. Defines the basic CRUD operations
 * for the notepad example, and gives the ability to list all notes as well as
 * retrieve or modify a specific note.
 * 
 * This has been improved from the first version of this tutorial through the
 * addition of better error handling and also using returning a Cursor instead
 * of using a collection of inner classes (which is less scalable and not
 * recommended).
 */
public class ProductsDbAdapter {

	//private static final String LOG_TAG = "ProductsDbAdapter";

	public static final String KEY_NAME = "name";
	public static final String KEY_UNIT_PRICE = "unit_price";
	public static final String KEY_MARKETID = "market_id";
	public static final String KEY_DESCRIPTION = "body";
	public static final String KEY_ROWID = "_id";

	public static final String DATABASE_TABLE = "products";
	/**
	 * Database creation sql statement
	 */
	static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + "  (_id integer primary key autoincrement, "
			+ "unit_price integer not null, "
			+ "market_id integer not null, "
			+ "name text not null, body text not null);";

	private SQLiteDatabase mDb;
	
	public ProductsDbAdapter(SQLiteDatabase mDb) {
		if (mDb == null)
			throw new IllegalArgumentException();
		this.mDb = mDb;
	}

	/**
	 * Create a new note using the title and body provided. If the note is
	 * successfully created return the new rowId for that note, otherwise return
	 * a -1 to indicate failure.
	 * 
	 * @param marketId
	 * @param name
	 *            the title of the note
	 * @param body
	 *            the body of the note
	 * @param unitPrice 
	 * @return rowId or -1 if failed
	 */
	public long createProduct(int marketId, String name, String body, int unitPrice) {
		// Verificar se o mercado existe no DB
		MarketDbAdapter marketDbAdapter = new MarketDbAdapter(mDb);
		marketDbAdapter.fetchMarket(marketId);

		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, name);
		initialValues.put(KEY_UNIT_PRICE, unitPrice);
		initialValues.put(KEY_MARKETID, marketId);
		initialValues.put(KEY_DESCRIPTION, body);
		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	/**
	 * Delete the note with the given rowId
	 * 
	 * @param rowId
	 *            id of note to delete
	 * @return true if deleted, false otherwise
	 */
	public boolean deleteProduct(long rowId) {
		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public boolean deleteAllProducts() {
		return mDb.delete(DATABASE_TABLE,null, null) > 0;
	}
	/**
	 * Return a Cursor over the list of all notes in the database
	 * 
	 * @return Cursor over all notes
	 */
	public Cursor fetchAllProducts() {
		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME,
				KEY_MARKETID, KEY_DESCRIPTION, KEY_UNIT_PRICE }, null, null, null, null, null);
	}

	/**
	 * Return a Cursor over the list of all notes in the database
	 * 
	 * @param marketId
	 * @return
	 */
	public Cursor fetchAllProducts(int marketId) {
		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME,
				KEY_MARKETID, KEY_DESCRIPTION, KEY_UNIT_PRICE }, KEY_MARKETID + "=" + marketId,
				null, null, null, null);
	}

	/**
	 * Return a Cursor positioned at the note that matches the given rowId
	 * 
	 * @param rowId
	 *            id of note to retrieve
	 * @return Cursor positioned to matching note, if found
	 * @throws SQLException
	 *             if note could not be found/retrieved
	 */
	public Cursor fetchProduct(long rowId) throws SQLException {
		Cursor result = mDb.query(true, DATABASE_TABLE, new String[] {
				KEY_ROWID, KEY_NAME, KEY_DESCRIPTION , KEY_UNIT_PRICE},
				KEY_ROWID + "=" + rowId, null, null, null, null);
		if ((result.count() == 0) || !result.first()) {
			throw new SQLException("No note matching ID: " + rowId);
		}
		return result;
	}

	/**
	 * Update the note using the details provided. The note to be updated is
	 * specified using the rowId, and it is altered to use the title and body
	 * values passed in
	 * 
	 * @param rowId
	 *            id of note to update
	 * @param name
	 *            value to set note title to
	 * @param description
	 *            value to set note body to
	 * @param unitPrice 
	 * @return true if the note was successfully updated, false otherwise
	 */
	public boolean updateProduct(long rowId, String name, String description, int unitPrice) {
		ContentValues args = new ContentValues();
		args.put(KEY_NAME, name);
		args.put(KEY_DESCRIPTION, description);
		args.put(KEY_UNIT_PRICE, unitPrice);
		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}
}
